import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";
import { NormalStepMoneyStrategy } from "./NormalStepMoneyStrategy";
import { FailUpMoneyStrategy } from "./FailUpMoneyStrategy";


export class WisdomMoneyStrategyV90 implements IMoneyStrategy {
    subPlan: IPlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV90Debug";
    strategyName: string;
    startIndex: number = -1;
    startLevel: number = 10000;
    constructor(name?: string) {
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        throw new Error("Method not implemented.");
    }
    resetRecm() {
        this.subPlan.forEach(item => item.resetRecm());
    }
    resetStrategy(): void {
        let plan1 = new ExcutePlan();
        plan1.recmStrategy = [new FixedStrategy({ reverse: false })]
        plan1.moneyStrategy = [new NormalStepMoneyStrategy("方案一", {
            baseMoney:10,
            startMoney: 100,
            addStep: 100,
            devideStep: -100,
            reverse: false,
            resetLevel: 800,
        }, true)];
        plan1.toResult(new RoundInfo());

        let plan2 = new ExcutePlan();
        plan2.recmStrategy = [new FixedStrategy({ reverse: false })]
        plan2.moneyStrategy = [new NormalStepMoneyStrategy("方案二", {
            baseMoney:10,
            startMoney: 100,
            addStep: 100,
            devideStep: -100,
            reverse: true,
            resetLevel: 800,
        }, true)];
        plan2.toResult(new RoundInfo());

        let array = [100, 200, 400, 800, 1600];
        let plan3 = new ExcutePlan();
        plan3.recmStrategy = [new FixedStrategy({ reverse: false })]
        plan3.moneyStrategy = [new FailUpMoneyStrategy("方案三", {
            reverse: false,
            extendWay: 1,
            betArray: array,
            baseMoney: 100,
            groupCount: 2
        }, true)];
        plan3.toResult(new RoundInfo());

        let plan4 = new ExcutePlan();
        plan4.recmStrategy = [new FixedStrategy({ reverse: false })]
        plan4.moneyStrategy = [new FailUpMoneyStrategy("方案四", {
            reverse: true,
            extendWay: 1,
            betArray: array,
            baseMoney: 100,
            groupCount: 2
        }, true)];
        plan4.toResult(new RoundInfo());

        this.subPlan = [plan1, plan2, plan3, plan4]

    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let sumWin = 0;

        this.subPlan.forEach((pl, index) => {
            pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);
            let userRecmV = pl.toResult(roundInfo);
            let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
            pl.userRecmInfo.recmV = userRecmV;
            let win = pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney;

            let [rv, rm] = pl.toMoney(newRoundInfo, recmInfo, userRecm, pl.baseMoney);
            pl.recmInfo.preBetCount = pl.recmInfo.betCount;
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;
            if (rv != EnumBjlResult.U) {
                //pl.recmInfo.
                pl.recmInfo.betCount++;
            }

            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += pl.recmInfo.recmMoney;

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += pl.recmInfo.recmMoney;
            }

        });
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }


        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV90;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.startIndex = obj.startIndex;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}